import time


import jwt # type: ignore


from flask import current_app




'''
   生成token
'''
def generate_token(data):
  data.update({'exp': time.time()+10})
  # 创建token
  token = jwt.encode(data, current_app.config['SECRET_KEY'], algorithm='HS256')
  return token
def verify_token(token):
  try:
    data = jwt.decode(token, current_app.config['SECRET_KEY'], algorithms=['HS256'])
  except Exception as e:
    return None
  return data
  
# 生成token
def login_required(view_func):
    @wraps(view_func)
    def verify_token_info(*args,**kwargs):
        #获取用户的token
        token = request.headers.get('token')
        #解析tonken
        if verify_token(token):
            return view_func(*args,**kwargs)
        else:
            return{'status':400,'msg':'token 无效'}
        #返回函数
    return verify_token_info